<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '340px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // this.setOptions(this.chartData)
      this.setOptions()
    },
    // setOptions({ expectedData } = {}) {
    setOptions() {
      this.chart.setOption({
        // backgroundColor: '#fff',
        // color: ['#3398DB'],
        title: {
          text: '当日诊断用时排名前5的医院',
          textStyle: {
            color: '#333',
            fontSize: '16'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          data: ['平均量', '当日平均量'],
          textStyle: {
            color: '#333333',
            fontSize: '14px'
          },
          right: '1%'
          // top: '%'
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
          show: false,
          backgroundColor: 'transparent',
          borderColor: 0
        },
        xAxis: [
          {
            type: 'category',
            data: ['北京潞河医院', '北京海淀医院', '北京顺义医院', '北京朝阳妇幼', '北京垂杨柳医院'],
            axisLine: { // 轴线
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              textStyle: {
                color: '#4A5A7F' // 坐标的字体颜色
              }
            },
            splitLine: {
              show: false
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisLine: { // 轴线
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              textStyle: {
                color: '#4A5A7F' // 坐标的字体颜色
              }
            }
          }
        ],
        series: [
          {
            name: '平均量',
            type: 'bar',
            barWidth: '8%',
            itemStyle: {
              normal: {
                barBorderRadius: 30,
                backgroundColor: 'transparent',
                // 颜色渐变
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: '#93EDC7'
                }, {
                  offset: 1,
                  color: '#00C9FF'
                }])
              }

            },
            data: [100, 502, 200, 334, 390]
          },
          {
            name: '当日平均量',
            type: 'bar',
            barWidth: '8%',
            itemStyle: {
              normal: {
                barBorderRadius: 30,
                backgroundColor: 'transparent',
                // 颜色渐变
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: '#BCAEEA'
                }, {
                  offset: 1,
                  color: '#7C5EF4'
                }])
              }

            },
            data: [100, 502, 200, 334, 390]
          }

        ]
      })
    }
  }
}
</script>
